package com.hope;

import android.support.v4.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

public class DailyPillFragment extends ListFragment {
	private EfficientAdapter adapter;
    private final String ARG_OBJECT = "object";
    private ImageButton left_button;
    private ImageButton right_button;
    static String[][] temp_array;
    private static int red = 0xFFC11B17;
    private static int green = 0xFF347C17;

    public DailyPillFragment() {
    	
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // The last two arguments ensure LayoutParams are inflated
        // properly.
        View rootView = inflater.inflate(R.layout.daily_pill_object, container, false);
        Bundle args = getArguments();
        ((TextView) rootView.findViewById(R.id.text1)).setText((args.getString(ARG_OBJECT)));
        
        left_button = (ImageButton) rootView.findViewById(R.id.left_button);
        left_button.setOnClickListener(new OnClickListener () {

			@Override
			public void onClick(View v) {
				ViewPager vp = (ViewPager) getActivity().findViewById(R.id.pager);
				vp.setCurrentItem(vp.getCurrentItem() - 1);
			}
        	
        });
        
        right_button = (ImageButton) rootView.findViewById(R.id.right_button);
        right_button.setOnClickListener(new OnClickListener () {

			@Override
			public void onClick(View v) {
				ViewPager vp = (ViewPager) getActivity().findViewById(R.id.pager);
				vp.setCurrentItem(vp.getCurrentItem() + 1);
			}
        	
        });
        
        String[][] temp_temp_array = {{"Paracetamol", "8:00 A.M."},{"Clonidine", "6:00 P.M."}};
        temp_array = temp_temp_array;
        adapter = new EfficientAdapter(getActivity().getApplicationContext());
        setListAdapter(adapter);
        
        return rootView;
    }
    
    public String getArgObject() {
    	return ARG_OBJECT;
    }
    
    private static class EfficientAdapter extends BaseAdapter {
    	private Context context;
    	
        public EfficientAdapter(Context con) {
        	context = con;
            LayoutInflater.from(context);
        }

        public int getCount() {
            return temp_array.length;
        }

        public Object getItem(int position) {
            return super.getItemViewType(position);
        }

        public long getItemId(int position) {
            return position;
        }
        
        public String getItemName(int position) {
        	return temp_array[position][0];
        }
        
        public String getItemTime(int position) {
        	return temp_array[position][1];
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;

            if (convertView == null) {
            	LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
            	convertView = inflater.inflate(R.layout.daily_pill_list_element, parent, false);
                
                holder = new ViewHolder();
                holder.name = (TextView) convertView.findViewById(R.id.pill_name);
                holder.time = (TextView) convertView.findViewById(R.id.pill_time);
                holder.taken_indicator = (TextView) convertView.findViewById(R.id.pill_taken_indicator);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.name.setText(getItemName(position));
            holder.time.setText(getItemTime(position));
            convertView.setOnClickListener(new OnItemClickListener());
            if (holder.taken)
        		holder.taken_indicator.setBackgroundColor(green);
        	else
        		holder.taken_indicator.setBackgroundColor(red);
            return convertView;
        }

        static class ViewHolder {
            TextView name;
            TextView time;
            TextView taken_indicator;
            boolean taken = false;
        }
        
        private class OnItemClickListener implements OnClickListener {
            OnItemClickListener() {
        	
            }
            
            public void onClick(View view) {
            	ViewHolder holder = (ViewHolder) view.getTag();
            	holder.taken = !holder.taken;
            	if (holder.taken)
            		holder.taken_indicator.setBackgroundColor(green);
            	else
            		holder.taken_indicator.setBackgroundColor(red);
            }	
        }
	}
}